package io.jft.doll.ui.share

import android.arch.lifecycle.ViewModelProviders
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.g.base.extend.observeEx
import com.g.base.extend.resultNotNull
import com.g.base.extend.setMargin
import com.g.base.help.QRHelp
import com.g.base.room.entity.UserEntity
import com.g.base.room.repository.Status
import com.g.base.ui.BaseActivity
import io.jft.doll.R
import io.jft.doll.databinding.ActivityShareBinding
import io.jft.doll.model.viewModel.UserViewModel
import io.jft.doll.room.entity.remote.InviteInfoREntity
import io.jft.doll.router.RouterPage
import io.jft.doll.wxapi.wxBitmapShare


@Route(path = RouterPage.InviteCodeActivity)
class ShareActivity : BaseActivity<ActivityShareBinding>() {
    override var isFullScreen: Boolean = true
    override var hasToolbar: Boolean = false

    private val userViewModel by lazy { ViewModelProviders.of(this).get(UserViewModel::class.java) }
    private var data: InviteInfoREntity? = null
    private var hasStartBottomAnimate = false
    private var enterAnimateHasComplete = false


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_share)
        contentView.data = this
        obsData()
    }

    override fun onTokenChange(data: UserEntity?) {
        if (data == null)
            onReload()
        else
            showNeedOauth()
    }

    override fun onReload() {
        userViewModel.operateInviteInfo()
    }

    private fun obsData() {
        userViewModel.operateInviteInfo()
        userViewModel.obsInviteInfo()
                .resultNotNull()
                .observeEx(this) {
                    when (it.status) {
                        Status.Loading -> showLoading()
                        Status.Content -> {
                            data = it.data!!
                            initView()
                        }
                        else -> showError()
                    }
                }
    }

    private fun initView() {
        val data = data ?: return
        contentView.invite.text = data.code
        contentView.qrCode.setImageBitmap(QRHelp.createQRCodeWithLogo(data.qrContent, 300, BitmapFactory.decodeResource(resources, R.drawable.ic_logo)))
        showContentView()
        startBottomAnimate()
    }

    override fun onEnterAnimationComplete() {
        enterAnimateHasComplete = true
        startBottomAnimate()
    }

    private fun startBottomAnimate() {
        if (hasStartBottomAnimate || !enterAnimateHasComplete || data == null) return
        hasStartBottomAnimate = true
        contentView.shareOperate.animate().withEndAction {
            contentView.scrollView.setMargin(bottom = contentView.shareOperate.height)
        }.translationY(0f).start()
    }

    fun shareWx(boolean: Boolean) {
        val shareContent = contentView.shareContent
        val bitmap = Bitmap.createBitmap(shareContent.width, shareContent.height, Bitmap.Config.ARGB_8888)
        shareContent.draw(Canvas(bitmap))
        wxBitmapShare(bitmap, boolean)
    }
}